package ru.alt22.visor.php.core.reflection;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.php.internal.core.phpModel.PHPModelUtil;
import org.eclipse.php.internal.core.phpModel.parser.PHPWorkspaceModelManager;
import org.eclipse.php.internal.core.phpModel.phpElementData.CodeData;
import org.eclipse.php.internal.core.phpModel.phpElementData.PHPClassData;

import ru.alt22.visor.php.properties.Properties;


public class ReflectionUtils {

	public static IProject getProjectFor(PHPClassData phpClass) {
		return PHPModelUtil.getResource(phpClass).getProject();
	}

	public static PHPClassData findClassByName(IProject project, String className) {
		CodeData[] classes = PHPWorkspaceModelManager.getInstance().getModelForProject(project).getClasses(className);
		if (classes == null || classes.length == 0) {
			return null;
		}
		for (int i = 0; i < classes.length; i++) {
			if (classes[i].getName().compareToIgnoreCase(className) == 0) {
				return (PHPClassData) classes[i];
			}
		}
		return (PHPClassData) classes[0];
	}

	public static String getTypeNamePrefix(IFolder folder) {
		IFolder sourceFolder = Properties.getSourceFolderFor(folder);
		if (sourceFolder == null) {
			return null;
		}
		IPath folderPath = folder.getProjectRelativePath();
		StringBuffer prefix = new StringBuffer();
		while (!sourceFolder.getProjectRelativePath().equals(folderPath)) {
			prefix.insert(0, folderPath.lastSegment() + "_");
			folderPath = folderPath.removeLastSegments(1);
		}
		return prefix.toString();
	}

}